<?php

namespace app\vip_agent\controller;

use PHPExcel_IOFactory;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Transaction extends Base
{
    // 佣金汇总 日期
    public function showsum(Request $request){
        $get['start']=isset($get['start'])?$get['start']:date('Y-m-d',strtotime('monday last week'));
        $get['end']=isset($get['end'])?$get['end']:date('Y-m-d',strtotime("friday last week"));
        return view('transaction/showsum',['get'=>$get]);
    }
    // 佣金汇总 数据
    public function showsummary(Request $request)
    {
        $get=$request->get();
        $get['p']=isset($get['p'])?$get['p']:config('paginate.list_rows');
        $get['start']=isset($get['start'])?$get['start']:date('Y-m-d',strtotime('monday last week'));
        $get['end']=isset($get['end'])?$get['end']:date('Y-m-d',strtotime("friday last week"));
        $jystart=str_replace('-','',$get['start']);
        $jyend=str_replace('-','',$get['end']);
//        dump($jyend);die;
        $data=Db::name('transaction')->alias('a')
            ->join('lc_agents b','a.agents=b.id')
            ->field('agents,b.id,b.name,sum(Count) Count,sum(Poundage) Poundage,sum(`Float`) myFloat,sum(Brokerage) Brokerage,sum(Poundage)-sum(Brokerage) fj')
            ->where('TradingDday','between',[$jystart,$jyend])
            ->where('a.vip',Session::get('user.id'))
            ->group('agents')
            ->paginate($get['p'],false,['query'=>$get]);

        $page=$data->render();
        $list=$data->items();
        $count['Count']=Db::name('transaction')->where('TradingDday','between',[$jystart,$jyend])->where('vip',Session::get('user.id'))->sum('Count');
        $count['Poundage']=Db::name('transaction')->where('TradingDday','between',[$jystart,$jyend])->where('vip',Session::get('user.id'))->sum('Poundage');
        $Float=Db::name('transaction')
            ->where('TradingDday','between',[$jystart,$jyend])
            ->where('vip',Session::get('user.id'))
            ->column('`Float`');
        $count['Float'] = 0;
        foreach ($Float as $v){
            $count['Float'] += $v;
        }
        $count['Brokerage']=Db::name('transaction')->where('TradingDday','between',[$jystart,$jyend])->where('vip',Session::get('user.id'))->sum('Brokerage');
        $count['fj']=$count['Poundage']-$count['Brokerage'];
        return view('transaction/showsummary',['list'=>$list,'page'=>$page,'get'=>$get,'count'=>$count]);
    }

    public function xq(Request $request)
    {
        $id=$request->get('id');
        $list=Db::name('transaction')->find($id);
        return $list;
    }




    // 入金统计数据
    public function rjsummary(Request $request)
    {
//        echo 111;die;
        $get=$request->get();
        $get['start']=isset($get['start'])?$get['start']:date('Y-m-d',strtotime('monday last week'));
        $get['end']=isset($get['end'])?$get['end']:date('Y-m-d',strtotime("friday last week"));
        $get['p']=isset($get['p'])?$get['p']:config('paginate.list_rows');
        $jystart=str_replace('-','',$get['start']);
        $jyend=date('Y-m-d',strtotime($get['end'].'+1 day'));
        $data=Db::name('cashlog')->alias('a')
            ->join('lc_users b','a.uid=b.id')
            ->join('lc_agents c','c.id=b.aid')
//            ->field('sum(price),c.id,c.name')
            ->field('
            sum(case when type = 5 then price else 0 end) rj,
            sum(case when type = 6 then price else 0 end) cj,
            c.id,c.name')
            ->where('a.time','between',[$jystart,$jyend])
            ->where('b.vid',session('user.id'))
            ->group('c.id')
            ->paginate($get['p'],false,['query'=>$get]);
        $list=$data->items();
        $page=$data->render();
        return view('transaction/cj/showsummary',['list'=>$list,'page'=>$page,'get'=>$get]);
    }

    // 出入金统计
    public function crsummary(Request $request)
    {
        $get=myarr($request->get());
        $aid=$get['aid'];
        $get['p']=isset($get['p'])?$get['p']:config('paginate.list_rows');
        $get['start']=isset($get['start'])?$get['start']:date('Y-m-d',strtotime('-1 day'));
        $get['end']=isset($get['end'])?$get['end']:date('Y-m-d');
        //$get['p']=isset($get['p'])?$get['p']:config('paginate.list_rows');
        $jystart=str_replace('-','',$get['start']);
        $jyend=date('Y-m-d',strtotime($get['end'].'+1 day'));
        $get['name']=isset($get['name'])?trim($get['name']):'';
        if($get['name']!=''){
            $where['name']=['=',$get['name']];
        }
        if($get['start']!=''&&$get['end']!=''){
            $where['time']=['between',[$jystart,$jyend]];
        }
        $where['aid']=['=',$aid];
        $list=Db::name('cashlog')->alias('a')
            ->join('lc_users b','a.uid=b.id')
            ->field('a.*,b.name')
            ->where($where)
            ->where('type','in','5,6')
            ->where('b.vid',session('user.id'))
            ->order('id desc')
//            ->select();
            ->paginate($get['p'],false,['query'=>$get]);
//        dump($list);die;
        $page=$list->render();
        $list=$list->items();
        $userall=Db::name('users')->field('id')->where('name','like','%'.$get['name'].'%')->where('aid',$aid)->column('id');
        $tj['rj']=Db::name('cashlog')->alias('a')
            ->join('lc_users b','a.uid=b.id')
            ->field('a.*,b.name')
            ->where($where)
            ->where('type',5)
            ->sum('price');
        $tj['cj']=Db::name('cashlog')->alias('a')
            ->join('lc_users b','a.uid=b.id')
            ->field('a.*,b.name')
            ->where($where)
            ->where('type',6)
            ->sum('price');
//        dump($list);die;
        return view('transaction/showsummarys',['list'=>$list,'page'=>$page,'get'=>$get,'tj'=>$tj]);
    }
}